#include "common.h"
#include <pthread.h>
#include <fcntl.h>
#include<sys/stat.h>
#include<dirent.h>
#include<sys/wait.h>
static char buffer[100][50];
static int song_num;
static int now_num;
int  get_song_list()
{
	
	struct dirent* dirp;
	DIR* dir;
	dir = opendir("./");
	if(dir <0)
		err_sys("open dirent error");
	dirp = readdir(dir);
	if(dirp < 0 )
		err_sys("read dirent error");
	while(dirp != NULL)
	{
		if(strstr(dirp->d_name,".mp3") != NULL)
		{
			strcpy(buffer[song_num],dirp->d_name);
		    song_num++;
		}
	    dirp = readdir(dir);
	}
	if(song_num == 0)
	err_sys("no such file");
	return 0;
}
void* read_song_message(void *arg)
{
    int f  ;
	f = (int)arg;
	int n;

	char data[2048];
	bzero(data,sizeof(data));
	while(1)
	{
		sleep(5);
		n =read(f,data,sizeof(data));
        if(n < 0)
		{
			err_sys("read fd[0] error");
		}
		else 
		{
			printf(" %s\n",data);
			bzero(data,sizeof(data));
		}
		
	}


	
	
	return NULL;
}



       
void* get_song_detail(void* arg)
{
	int f ;
	f = open("mplayerfifo",O_WRONLY);
	if (f<0)
	{
		err_sys("open fifo error");
	}
    char cmd[100] ;
	char str[100];
	bzero(cmd,sizeof(cmd));
	bzero(str,sizeof(str));
    char  cmd_list[6][50]={"seek 15","seek -15","pause","mute 1","mute 0","loadfile"};
    char  cmd_list1[6][50]={"quick","slow","start","quiet","noquiet","next"};
	int ret = 0;
	while(1)
	{
	    int i = 0;
		printf("input your cmd:\n");
        //sleep(10);
		fgets(cmd,sizeof(cmd),stdin);
       while(i<6)
	   {

		  ret = strncmp(cmd,cmd_list1[i],strlen(cmd)-1);
		  if(ret == 0)
		  {
			  strcpy(str,cmd_list[i]);
			  if(i == 5)
			  {
				  printf("%d\n",now_num);
				  now_num--; 
				  strcat(str,"\t");
				  strcat(str,buffer[now_num]);
				  printf("name is %s\n",buffer[now_num]);
				  strcat(str,"\n");

			  }
			  else
			  {
				  strcat(str,"\n");

			  }
		  }
		  i=i+1;
	   }



        
		write(f,str,strlen(str));
		sleep(2);
		write(f,"get_time_pos\n",strlen("get_time_pos\n"));
		sleep(2);
		write(f,"get_percent_pos\n",strlen("get_percent_pos\n"));
		sleep(2);
        write(f,"get_file_name\n",strlen("get_file_name\n"));
     
		bzero(cmd,sizeof(cmd));
		bzero(str,sizeof(str));
        sleep(5);
	}	

    return NULL;
	}


   
	

static void print(char* filename)
{
	get_song_list();
	printf("%d\n",song_num);
	int i = 0;
	int re;
	
	for(i = 0;i<song_num;i++)
	{
		re = strcmp(buffer[i],filename);
		if(re == 0)
		 {
			 now_num = i;
			 fprintf(stdout,"\033[31m\033[1m");
			 fprintf(stdout,"%s\n",filename);
			 fprintf(stdout,"\033[0m");
		 }
		else
		{
			printf("%s\n",buffer[i]);
		}


	}
	return ;
}
int main (int argc,char* argv[])
{
	if(argc < 2)
		err_sys("no file name");

		print(argv[1]);
    int	ret = 0;
	pid_t pid;
	pthread_t tid1;
	pthread_t tid2;


	int fd[2];
	
	ret = pipe(fd);
	if(ret<0)
		err_sys("pipe error");
	ret = access("mplayerfifo",F_OK);
		if(ret == 0)
		{
			unlink("mplayerfifo");
			mkfifo("./mplayerfifo",0755);
		}
		else
		{
			ret = mkfifo("./mplayerfifo",0755);
			if(ret < 0)
			err_sys("make fifo error");
		}
	pid = fork();
	if(pid <0)
		err_sys("process create error");
	else if(pid == 0)
	{
		close(fd[0]);
		close(1);

		dup2(fd[1],1);
		execlp("mplayer","mplayer","-slave","-quiet","-idle","-input","file=./mplayerfifo",buffer[now_num],NULL);
		

	}
	else
	{
	    close(fd[1]);
		pthread_create(&tid1,NULL,read_song_message,(void*)fd[0]);
		pthread_create(&tid2,NULL,get_song_detail,NULL);
	    pthread_join(tid2,NULL);
		pthread_join(tid1,NULL);
		wait(NULL);
		
	}
	


return 0;

}
	





